<?php
function baseArmourSort($a, $b) {
    return $b->baseArmour == $a->baseArmour ? 0 : $b->baseArmour > $a->baseArmour ? -1 : 1;
}

function adjustedSort($a, $b) {
    return $b->armour == $a->armour ? 0 : $b->armour > $a->armour ? -1 : 1;
}

function casualtiesSort($a, $b) {
    return $b->casAg == $a->casAg ? 0 : $b->casAg > $a->casAg ? -1 : 1;
}

function baseRatioSort($a, $b) {
    return $b->baseRatio == $a->baseRatio ? 0 : $b->baseRatio < $a->baseRatio ? -1 : 1;
}

function adjustedRatioSort($a, $b) {
    return $b->armourRatio == $a->armourRatio ? 0 : $b->armourRatio < $a->armourRatio ? -1 : 1;
}

class ArmourRanking {
    var $baseArmour = 0;
    var $armour = 0;
    var $casAg;
    var $link;
    var $baseRatio = 0;
    var $armourRatio = 0;

    function ArmourRanking() {
    }
}

function getArmourRanking($team, & $row) {
    $armour = 0;
    $baseArmour = 0;
    $count = sizeof($team->players);
    $ranking = new ArmourRanking();
    $ranking->link = $team->getRosterLink();
    $ranking->casAg = $team->currentSeasonStats->getAverageSuffered();
    if ($count == 0) {
        return $ranking;
    }
    foreach ($team->players as $player) {
        $prob = getProb(1 + $player->av);
        $baseArmour += $prob;

        if (ereg("^[B|b]lock", $player->skills) or ereg(", [b|B]lock,", $player->skills)) {
            $prob = $prob * 29 / 36;
        }

        if (ereg("[d|D]odge", $player->skills)) {
            $prob = (($prob * 29 / 36) * (3 / 4)) + ($prob / 4);
        }

        $armour += $prob;

        if ($player->st > 3) {
            $armour -= ($player->st - 2);
        } else
            if ($player->st < 3) {
                $armour += 2;
            }
    }

    $ranking->armour = round((100 * $armour) / ($count * 36), 2);
    $ranking->baseArmour = round((100 * $baseArmour) / ($count * 36), 2);
    $ranking->armourRatio = round($ranking->armour / ($ranking->casAg == 0 ? 1 : $ranking->casAg), 2);
    $ranking->baseRatio = round($ranking->baseArmour / ($ranking->casAg == 0 ? 1 : $ranking->casAg), 2);

    return $ranking;
}

$showStats = true;
include_once ("includes/init.php");
$title = $season->season . " Armour";
include_once ("includes/header.php");

$rankings = array ();
$totals = new ArmourRanking();
$totals->link = "Average";
$count = 0;
foreach ($teams->teams as $team) {
    $temp = getArmourRanking($team, $row);
    if ($temp->baseArmour > 0) {
        $count++;
        $totals->baseArmour += $temp->baseArmour;
        $totals->armour += $temp->armour;
        $totals->casAg += $temp->casAg;
        $totals->baseRatio += $temp->baseRatio;
        $totals->armourRatio += $temp->armourRatio;
    }
    array_push($rankings, $temp);
}
if ($count < 1) {
    $count = 1;
}
$totals->baseArmour = round($totals->baseArmour / $count, 2);
$totals->armour = round($totals->armour / $count, 2);
$totals->casAg = round($totals->casAg / $count, 2);
$totals->baseRatio = round($totals->baseRatio / $count, 2);
$totals->armourRatio = round($totals->armourRatio / $count, 2);
array_push($rankings, $totals);

$sort = getParam("sort");
switch ($sort) {
    case 1 :
        uasort($rankings, "baseArmourSort");
        break;
    case 2 :
        uasort($rankings, "adjustedSort");
        break;
    case 3 :
        uasort($rankings, "casualtiesSort");
        break;
    case 4 :
        uasort($rankings, "baseRatioSort");
        break;
    case 5 :
        uasort($rankings, "adjustedRatioSort");
        break;
}

echo<<< EOQ
      <caption><h1>Team Casualties</h1></caption>
      <tr>
        <th align="left">Team</th>
        <th><a href="armour.php?sort=1">Armour&nbsp;%</a></th>
        <th><a href="armour.php?sort=2">Adjusted&nbsp;%</a></th>
        <th><a href="armour.php?sort=3">Ave&nbsp;Cas</a></th>
        <th><a href="armour.php?sort=4">Armour Ratio</a></th>
        <th><a href="armour.php?sort=5">Adjusted Ratio</a></th>
      </tr>
EOQ;
$row = 0;
foreach ($rankings as $ranking) {
    echo getEchoTR(++ $row);
    echo<<< EOQ
        <td>$ranking->link</td>
        <td align="center">$ranking->baseArmour</td>
        <td align="center">$ranking->armour</td>
        <td align="center">$ranking->casAg</td>
        <td align="center">$ranking->baseRatio</td>
        <td align="center">$ranking->armourRatio</td>
      </tr>
EOQ;
}
echo<<< EOQ
  <tr>
    <td colspan="6"><br />
      <ul>
        <li><b>Armour %</b> This is the average chance of the teams armour being penerated. So if every player in a team had Av 9, the value would be 16.67%, if it were Av 8 27.78%, or Av 7 41.67%</li>
        <br /><br /><li><b>Adjusted %</b> This is the armour % but is adjusted for skills and strength. It is assumed you are always being blocked with 2 dice, by a player with Block and a quarter the time he has tackle too. This means Blodge stops you going down about 35% of the time, block alone about 20% and dodge alone 15%. Strength makes a small arbitary difference.</li>
        <br /><br /><li><b>Ave Cas</b> The average number of casualties suffered in a game by a team.</li>
        <br /><br /><li><b>Armour Ratio</b> Armour % divided by average casualties. An above average ratio means that the team has suffered fewer casualties than expected, a below average ratio more casualties than expected.</li>
        <br /><br /><li><b>Adjusted Ratio</b> Adjusted % divided by average casualties. An above average ratio means that the team has suffered fewer casualties than expected, a below average ratio more casualties than expected.</li>
      </ul>
    </td>
  </tr>
EOQ;
include ("includes/footer.php");
?>
